package com.lightningtrade.controller;

import com.lightningtrade.domain.SysUser;
import com.lightningtrade.log.annotaion.Log;
import com.lightningtrade.log.enums.BusinessType;
import com.lightningtrade.service.SysUserService;
import com.lightningtrade.util.AjaxResult;
import com.lightningtrade.util.JwtTokenUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户管理-0312杜晟玮")
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Log(title = "更新用户信息", businessType = BusinessType.INSERT)
    @ApiOperation(value = "更新用户信息", notes = "更新用户的个人信息")
    @PutMapping("/update")
    public AjaxResult updateUserInfo(@RequestBody SysUser sysUser) {
        int result = sysUserService.updateUserInfo(sysUser);
        if (result > 0) {
            return AjaxResult.success("用户信息更新成功");
        }
        return AjaxResult.error("更新失败");
    }

    @ApiOperation(value = "更新用户角色", notes = "更新用户的角色")
    @PutMapping("/role")
    public AjaxResult updateUserRole(@RequestParam Integer uid, @RequestParam String role) {
        int result = sysUserService.updateUserRole(uid, role);
        if (result > 0) {
            return AjaxResult.success("用户角色更新成功");
        }
        return AjaxResult.error("更新失败");
    }

    @Log(title = "注销用户", businessType = BusinessType.OTHER)
    @ApiOperation(value = "注销用户", notes = "注销用户账户")
    @DeleteMapping("/delete")
    public AjaxResult deleteUser(@RequestParam Integer uid) {
        int result = sysUserService.deleteUser(uid);
        if (result > 0) {
            return AjaxResult.success("用户注销成功",null);
        }
        return AjaxResult.error("注销失败");
    }

    @Log(title = "修改密码", businessType = BusinessType.OTHER)
    @ApiOperation(value = "修改密码", notes = "通过手机号和密码设置新密码")
    @PutMapping("/recoverPassword")
    public AjaxResult recoverPassword(@RequestParam String phone, @RequestParam String oldPassword, @RequestParam String newPassword) {
        int result = sysUserService.recoverPassword(phone, oldPassword, newPassword);
        if (result > 0) {
            return AjaxResult.success("密码重置成功");
        }
        return AjaxResult.error("密码重置失败");
    }
}
